/**
 * 设备树形菜单查询
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");

module.exports = async function (params) {
    let sql = `
    WITH recursive range_tree AS (
        SELECT
        pdf.range_id AS rangeId,
        rf.parent_id AS rangeParentId,
        rf.range_name AS rangeName,
        rf.domain_id AS domainId,
        pdf.device_id AS pDeviceId,
        pdf.device_name AS pDeviceName,
        pdf.device_no AS pDeviceNo,
        pdf.device_type AS pDeviceType,
        pdf.enable AS pEnable,
        cdf.device_id AS cDeviceId,
        cdf.device_no AS cDeviceNo,
        cdf.device_type AS cDeviceType,
        cdf.parent_id AS cParentId,
        cdf.device_name AS cDeviceName,
        cdf.enable AS cEnable
    FROM
        device_file pdf
    INNER JOIN range_file rf ON rf.range_id = pdf.range_id
        AND rf.delete_sign = 1 AND rf.domain_id = ${params.userDomainId}
    LEFT JOIN device_file cdf ON cdf.parent_id = pdf.device_id
        AND cdf.delete_sign = 1
    WHERE pdf.delete_sign = 1 AND pdf.device_type = ${params.deviceType} AND pdf.parent_id = 0
    UNION
    SELECT
        rf.range_id AS rangeId,
        rf.parent_id AS rangeParentId,
        rf.range_name AS rangeName,
        rf.domain_id AS domainId,
        pdf.device_id AS pDeviceId,
        pdf.device_name AS pDeviceName,
        pdf.device_no AS pDeviceNo,
        pdf.device_type AS pDeviceType,
        pdf.enable AS pEnable,
        cdf.device_id AS cDeviceId,
        cdf.device_no AS cDeviceNo,
        cdf.device_type AS cDeviceType,
        cdf.parent_id AS cParentId,
        cdf.device_name AS cDeviceName,
        cdf.enable AS cEnable
    FROM
        range_file rf
    LEFT JOIN device_file pdf ON rf.range_id = pdf.range_id 
        AND pdf.parent_id = 0
        AND pdf.delete_sign = 1
        AND pdf.device_type = ${params.deviceType}
    LEFT JOIN device_file cdf ON cdf.parent_id = pdf.device_id 
        AND cdf.delete_sign = 1
    JOIN range_tree rt ON rt.rangeParentId = rf.range_id
    WHERE rf.delete_sign = 1 AND rf.domain_id = ${params.userDomainId}
)
    SELECT * FROM range_tree;
    SELECT
        df.device_id AS deviceId,
        df.device_no AS deviceNo,
        df.device_name AS deviceName,
        df.parent_id AS parentId
    FROM
    device_file df
    INNER JOIN range_file rf ON rf.range_id = df.range_id
    AND rf.delete_sign = 1
    AND rf.domain_id = ${params.userDomainId}
    INNER JOIN project_device_mid pdm ON pdm.device_id = df.device_id
    AND pdm.project_id = ${params.projectId}
    WHERE
    df.delete_sign = 1
    AND df.device_type = ${params.deviceType}`;


    let result = await conn(sql);
    let deviceTreeList = result[0];
    let deviceList = result[1];

    let rangeParentObj = {};
    let deviceInfoObj = {};
    let rangeGroup = _.groupBy(deviceTreeList,"rangeId");
    for(const rangeId in rangeGroup){
        let rangeList = rangeGroup[rangeId];
        let pDeviceGroup = _.groupBy(rangeList,"pDeviceId");
        let rangeObj = {
            id:"r_"+rangeId,
            label:rangeList[0].rangeName,
            rangeId:rangeId,
            parentId:rangeList[0].rangeParentId,
            isLeaf:false,
            disabled: true,
            children:[],
        }
        if(!pDeviceGroup["null"] ){
            for(const pDeviceId in pDeviceGroup){
                let pDeviceList = pDeviceGroup[pDeviceId];
                let cDeviceGroup = _.groupBy(pDeviceList,"cDeviceId");
                let pDeviceObj = {
                    id:pDeviceId,
                    label:pDeviceList[0].pDeviceName,
                    parentId:0,
                    isLeaf:true,
                    children:[],
                }
                let childrenIds = [];
                if(!cDeviceGroup["null"]) {
                    for (const cDeviceId in cDeviceGroup) {
                        let cDeviceList = pDeviceGroup[pDeviceId];
                        let cDeviceObj = {
                            id: cDeviceId,
                            label: cDeviceList[0].cDeviceName,
                            parentId:pDeviceId,
                            isLeaf: true,
                        }
                        childrenIds.push(cDeviceId);
                        pDeviceObj.children.push(cDeviceObj);
                    }
                }
                deviceInfoObj[`${pDeviceId}`] = childrenIds;
                rangeObj.children.push(pDeviceObj);
            }
        }
        if(!rangeParentObj[rangeObj.parentId]) rangeParentObj[`${rangeObj.parentId}`] = [];
        rangeParentObj[`${rangeObj.parentId}`].push(rangeObj);
    }
    let treeList = [];
    if(rangeParentObj[`0`]){
        treeList = await rangeInterval(rangeParentObj[`0`], rangeParentObj);
    }

    let checkDeviceIdList = [];
    let checkDeviceGroup = _.groupBy(deviceList,"parentId");
    if(checkDeviceGroup[`0`]){
        checkDeviceIdList = await deviceInterval(checkDeviceGroup[`0`], checkDeviceGroup);
    }
    return {success:true,deviceInfoObj:deviceInfoObj,checkDeviceIdList:checkDeviceIdList,treeList:treeList}

};
async function rangeInterval(rangeList, rangeParentObj) {
    for (const index in rangeList) {
        if (rangeParentObj[`${rangeList[index].rangeId}`]) {
            let rangeChild = await rangeInterval(rangeParentObj[`${rangeList[index].rangeId}`], rangeParentObj)
            rangeList[index].children = rangeList[index].children.concat(rangeChild);
        }
    }
    return rangeList;
}
async function deviceInterval(deviceList, deviceParentObj) {
    let deviceIdList = [];
    for (const index in deviceList) {
        if (deviceParentObj[`${deviceList[index].deviceId}`]) {
            let deviceChildIdList = await deviceInterval(deviceParentObj[`${deviceList[index].deviceId}`], deviceParentObj)
            deviceIdList = deviceIdList.concat(deviceChildIdList);
        }
        deviceIdList.push(deviceList[index].deviceId.toString())
    }
    return deviceIdList;
}